package com.example.demo.service.imply;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.annotation.AutoFill;
import com.example.demo.enumeration.OperationType;
import com.example.demo.mapper.ComponentMapper;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.dto.InventoryDTO;
import com.example.demo.pojo.entity.*;
import com.example.demo.pojo.entity.Inventory;
import com.example.demo.pojo.query.InventoryQuery;
import com.example.demo.pojo.vo.InboundVO;
import com.example.demo.pojo.vo.InventoryVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.IInventoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IInventoryServiceImply extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {
    private final InventoryMapper inventoryMapper;
    private final ComponentMapper componentMapper;
    private final StorageMapper storageMapper;
    @Override
    @AutoFill(OperationType.INSERT)
    public void addStorage(InventoryDTO inventoryDTO, PublicFields publicFields) {
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(inventoryDTO, inventory);
        BeanUtils.copyProperties(publicFields, inventory);
        baseMapper.insert(inventory);
    }

    @Override
    public void deleteInventory(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageVO<InventoryVO> queryInventoryPage(InventoryQuery inventoryQuery) {
        Page<Inventory> p = Page.of(inventoryQuery.getPageNo(), inventoryQuery.getPageSize());
        p.addOrder(OrderItem.desc("update_time"));
        PageVO<InventoryVO> inventoryVOs = new PageVO<>();
        LambdaQueryWrapper<Inventory> lambdaQuery = new LambdaQueryWrapper<>();
        Page<Inventory> page = lambdaQuery()
                .like(StringUtils.hasText(inventoryQuery.getStorageId()), Inventory::getStorageId, inventoryQuery.getStorageId())
                .like(StringUtils.hasText(inventoryQuery.getComponentId()), Inventory::getComponentId, inventoryQuery.getComponentId())
                .gt(inventoryQuery.getNumber() >= inventoryQuery.getNumber(), Inventory::getNumber, inventoryQuery.getNumber())
                .page(p);

        inventoryVOs.setPages(p.getPages());
        inventoryVOs.setTotal(p.getTotal());
        List<Inventory> records = p.getRecords();
        List<InventoryVO> InboundVOList = records.stream()
            .map(inventory -> {
            InventoryVO inventoryVO = new InventoryVO();
            BeanUtils.copyProperties(inventory, inventoryVO);
            Component component=componentMapper.selectById(inventory.getComponentId());
            Storage storage=storageMapper.selectById(inventory.getStorageId());
            inventoryVO.setComponentName(component.getComponentName());
            inventoryVO.setStorageName(storage.getStorageName());
            return inventoryVO;
        }).collect(Collectors.toList());
        inventoryVOs.setList(InboundVOList);
        return inventoryVOs;
    }

    @Override
    @AutoFill(OperationType.UPDATE)
    public void updateInventory(InventoryDTO inventoryDTO, PublicFields publicFields) {
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(inventoryDTO, inventory);
        BeanUtils.copyProperties(publicFields, inventory);
        LambdaUpdateWrapper<Inventory> wrapper = new LambdaUpdateWrapper<Inventory>()
                .set(inventory.getNumber() != null, Inventory::getNumber, inventory.getNumber())
                .eq(Inventory::getComponentId, inventory.getComponentId())
                .eq(Inventory::getStorageId, inventory.getStorageId());
        inventoryMapper.update(wrapper);
    }
    //查询连接表信息
    @Override
    public Inventory selectInventory(int componentId, int storageId) {
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getComponentId, componentId)
                .eq(Inventory::getStorageId, storageId);
        return inventoryMapper.selectOne(queryWrapper);
    }


}
